前端analysis | What,Why,Who,When,Where,How

《Dart》- 入门整理

2020-03-25

环境安装

安装

1
2
$ brew tap dart-lang/dart
$ brew install dart

验证

1
$ dart --version

版本命名

1
2
3
4
5
6
7
8
9
# 升级
$ brew upgrade dart

# 版本切换
$ brew switch dart 1.24.3
$ brew switch dart 2.1.0

# 查看版本
$ brew info dart

stagehand

1
2
3
# 自动创建dart工程
$ pub global activate stagehand
$ export PATH="$PATH":"$HOME/.pub-cache/bin"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 会在当前目录,快速生成代码
$ cd dart-demo
$ stagehand
Stagehand will generate the given application type into the current directory.

usage: stagehand <generator-name>
--[no-]analytics Opt out of anonymous usage and crash reporting.
-h, --help Help!
--version Display the version for stagehand.
--author The author name to use for file headers.
(defaults to "<your name>")

console-full - A command-line application sample.
console-simple - A simple command-line application.
package-simple - A starting point for Dart libraries or applications.
server-shelf - A web server built using the shelf package.
web-angular - A web app with material design components.
web-simple - A web app that uses only core Dart libraries.
web-stagexl - A starting point for 2D animation and games.

zip sdk包

sdk下载

语法 – 学习时候,请类比js or ts

coding注意事项

  • main函数不可缺
  • 需要;结尾

类型

  • 7种内置类型:strings,booleans,maps,lists,numbers,runes(字符),symbols(符号类型)
dart js ts
strings string string
booleans boolean boolean
maps map or set map or set
lists array or [] array or []
symbols symbol symbol
runes string string
numbers Number number
无对象 object object
自定义 function function
void
null null null
undefined undefined
=> => => 支持箭头函数
  • var 变量自动推断,支持显示声明7种类型。

  • var 变量,不赋值,则为null。非js种的undefined

  • numbers分,int,double类型。

  • 变量可以直接操作方法。类似js。

  • 参数、返回值是定义不同于ts

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //dart ,类似java\c
    int fibonacci(int n) {
    if (n == 0 || n == 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
    }

    //ts
    function getLength(something: string | number): number {
    return something.length;
    }
  • 引用包,只能使用import。js| ts,可以使用module.exports| require.

    1
    2
    3
    4
    5
    6
    7
    8
    // Importing core libraries
    import 'dart:math';

    // Importing libraries from external packages
    import 'package:test/test.dart';

    // Importing files
    import 'path/to/my_other_file.dart';

number

  • int

    1
    2
    3
    4
    var num = 1;
    print(num.runtimeType);
    print(num.isEven);
    print(num.bitLength);
  • strings

    1
    2
    3
    4
    # 类似python ,原始字符串
    var ss = r'hello \n world';
    print(ss);
    print(ss * 2); //copy
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏